package com.yb.xczx.cloud.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yb.core.enums.CourseBaseEnum;
import com.yb.core.enums.CourseMarketEnum;
import com.yb.core.enums.HttpCode;
import com.yb.core.exception.XczxBusinessException;
import com.yb.core.model.XczxResult;
import com.yb.core.web.db.model.XczxPage;
import com.yb.xczx.cloud.content.entity.CourseBase;
import com.yb.xczx.cloud.content.entity.CourseCategory;
import com.yb.xczx.cloud.content.entity.CourseMarket;
import com.yb.xczx.cloud.content.mapper.CourseBaseMapper;
import com.yb.xczx.cloud.content.service.ICourseBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yb.xczx.cloud.content.service.ICourseCategoryService;
import com.yb.xczx.cloud.content.service.ICourseMarketService;
import com.yb.xczx.cloud.content.vo.AddCourseVo;
import com.yb.xczx.cloud.content.vo.CourseBaseInfoVo;
import com.yb.xczx.cloud.content.vo.CourseBaseVo;
import com.yb.xczx.cloud.content.vo.UpdateCourseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author 姚兵
 * @since 2023-05-07
 */
@Service
@Slf4j
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements ICourseBaseService {

    @Autowired
    ICourseMarketService courseMarketService;


    @Autowired
    ICourseCategoryService courseCategoryService;
    @Override
    public XczxResult page(XczxPage<CourseBaseVo> xczxPage) {
//        throw new XczxBusinessException(101,"hah");
        if(Objects.isNull(xczxPage)){
            return XczxResult.error(HttpCode.Response.BAD_REQUEST);
        }

        CourseBaseVo conditionVo = xczxPage.getCondition();

        XczxPage<CourseBase> baseXczxPage = new XczxPage<>();
        baseXczxPage.setOrders(xczxPage.getOrders());
        baseXczxPage.setCurrent(xczxPage.getCurrent());
        baseXczxPage.setSize(xczxPage.getSize());

        XczxPage resultPage = this.page(baseXczxPage, buildQueryWrapper(conditionVo));

        return XczxResult.success(resultPage);
    }

    @Transactional
    @Override
    public CourseBaseInfoVo createCourseBase(Long companyId,AddCourseVo courseVo) {

        CourseBase courseNew=new CourseBase();
        BeanUtils.copyProperties(courseVo,courseNew);
        courseNew.setCompanyId(companyId);
        courseNew.setCreateDate(LocalDateTime.now());
        //审核状态默认为未提交
        courseNew.setAuditStatus(CourseBaseEnum.AuditStatus.UNCOMMITTED.getCode());

        //发布状态，未发布
        courseNew.setStatus(CourseBaseEnum.PublishStatus.NOT_PUBLISH.getCode());
        //向courseBase表写入数据

        if(!this.save(courseNew)){
            throw new XczxBusinessException(HttpCode.Response.ERROR);
        }


        //向courseMarket营销表写入数据
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(courseVo,courseMarket);
        courseMarket.setId(courseNew.getId());


        //如果课程收费必须填写价格

        if(courseMarket.getCharge().equals(CourseMarketEnum.ChargeType.CHARGE.getCode())){
            Float price = courseMarket.getPrice();
            if(null==price||price.compareTo(0f)<=0){
                throw new XczxBusinessException(HttpCode.Response.CHARGE_PRICE_EMPTY);
            }
        }
        if(!courseMarketService.saveOrUpdate(courseMarket)){
            throw new XczxBusinessException(HttpCode.Response.ERROR);
        }


        //封装courseBaseInfoVo

        CourseBaseInfoVo courseBaseInfoVo = new CourseBaseInfoVo();
        BeanUtils.copyProperties(courseNew,courseBaseInfoVo);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoVo);

        //TODO 课程分类名称设置到courseBaseInfoVo
        String mt = courseBaseInfoVo.getMt();
        String st = courseBaseInfoVo.getSt();
        Map<String,CourseCategory> categoryMap=courseCategoryService.getByMtSt( mt, st);

        courseBaseInfoVo.setMtName(categoryMap.get(mt).getName());
        courseBaseInfoVo.setStName(categoryMap.get(st).getName());
        return courseBaseInfoVo;
    }

    @Override
    public CourseBaseInfoVo getCourseBaseInfo(String courseId) {
        CourseBase courseBase = this.getById(courseId);

        CourseMarket courseMarket = courseMarketService.getById(courseId);

        CourseBaseInfoVo courseBaseInfoVo = new CourseBaseInfoVo();
        BeanUtils.copyProperties(courseBase,courseBaseInfoVo);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoVo);
        return courseBaseInfoVo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public CourseBaseInfoVo updateCourseBase(Long companyId,  UpdateCourseVo courseVo) {
        CourseBase courseUpdate=new CourseBase();
        BeanUtils.copyProperties(courseVo,courseUpdate);
        courseUpdate.setCompanyId(companyId);
        courseUpdate.setChangeDate(LocalDateTime.now());
//        //审核状态默认为未提交
//        courseUpdate.setAuditStatus(CourseBaseEnum.AuditStatus.UNCOMMITTED.getCode());
//
//        //发布状态，未发布
//        courseUpdate.setStatus(CourseBaseEnum.PublishStatus.NOT_PUBLISH.getCode());
        //向courseBase表写入数据

        if(!this.updateById(courseUpdate)){
            throw new XczxBusinessException(HttpCode.Response.ERROR);
        }


        //向courseMarket营销表写入数据
        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(courseVo,courseMarket);
        courseMarket.setId(courseUpdate.getId());


        //如果课程收费必须填写价格

        if(courseMarket.getCharge().equals(CourseMarketEnum.ChargeType.CHARGE.getCode())){
            Float price = courseMarket.getPrice();
            if(null==price||price.compareTo(0f)<=0){
                throw new XczxBusinessException(HttpCode.Response.CHARGE_PRICE_EMPTY);
            }
        }
        if(!courseMarketService.saveOrUpdate(courseMarket)){
            throw new XczxBusinessException(HttpCode.Response.ERROR);
        }


        //封装courseBaseInfoVo

        CourseBaseInfoVo courseBaseInfoVo = new CourseBaseInfoVo();
        BeanUtils.copyProperties(courseUpdate,courseBaseInfoVo);
        BeanUtils.copyProperties(courseMarket,courseBaseInfoVo);

        //TODO 课程分类名称设置到courseBaseInfoVo
        String mt = courseBaseInfoVo.getMt();
        String st = courseBaseInfoVo.getSt();
        Map<String,CourseCategory> categoryMap=courseCategoryService.getByMtSt( mt, st);

        courseBaseInfoVo.setMtName(categoryMap.get(mt).getName());
        courseBaseInfoVo.setStName(categoryMap.get(st).getName());
        return courseBaseInfoVo;
    }


    private LambdaQueryWrapper buildQueryWrapper(CourseBaseVo courseBaseVo){

        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(courseBaseVo.getName()),CourseBase::getName,courseBaseVo.getName());
        queryWrapper.eq(StringUtils.hasText(courseBaseVo.getAuditStatus()),CourseBase::getAuditStatus,courseBaseVo.getAuditStatus());
        queryWrapper.eq(StringUtils.hasText(courseBaseVo.getStatus()),CourseBase::getStatus,courseBaseVo.getStatus());
        queryWrapper.orderByDesc(CourseBase::getCreateDate);
        return queryWrapper;

    }
}
